﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using BCGC.BLL.BusinessObject.Poll;
using Jalal.Library.Utility;
using Jalal.Library.DataStructure;
using BCGC.BLL.Helper;

public partial class Admin_Poll_Default : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetAnswer();
            BindGrid();
        }
        InitDatePicker();
    }

    private void InitDatePicker()
    {
        wucStartTime.YearFrom = DateTime.Now.Year;
        wucStartTime.YearTo = DateTime.Now.Year + 10;
        wucEndTime.YearFrom = DateTime.Now.Year;
        wucEndTime.YearTo = DateTime.Now.Year + 10;
    }

    protected void AnswerCount_Changed(object sender, EventArgs e)
    {
        SetAnswer();
    }

    private void SetAnswer()
    {
        List<int> items = new List<int>();
        int count = int.Parse(ddlAnswerCount.SelectedValue);
        for (int i = 1; i <= count; i++)
            items.Add(i);
        rptAnswerBoxes.DataSource = items;
        rptAnswerBoxes.DataBind();
    }

    protected void Create_Click(object sender, EventArgs e)
    {
        List<string> answers = new List<string>();
        string questionText = Security.BindStringInput(tbxQuestion.Text, SecurityLevel.Three);

        if (!PollQuestion.PollQuestionExists(questionText))
        {
            foreach (RepeaterItem item in rptAnswerBoxes.Items)
            {
                TextBox tbxAnswer = (TextBox)item.FindControl("tbxAnswer");
                if (tbxAnswer != null)
                {
                    string answerText = Security.BindStringInput(tbxAnswer.Text, SecurityLevel.Three);
                    answers.Add(answerText);
                }
            }

            PollQuestion question = PollQuestion.Create(questionText, answers);
            BindGrid();
        }
    }

    private void BindGrid()
    {
        ReadWriteCustomCollection<PollQuestion> allQuestions = PollQuestion.GetQuestionsBySearch(null);
        grvPollQuestions.DataSource = allQuestions;
        grvPollQuestions.DataBind();
    }

    protected void GridViewDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox cbxSelectAll = (CheckBox)e.Row.FindControl("cbxSelectAll");
            cbxSelectAll.Attributes.Add("onclick", "javascript:SelectAll('" + grvPollQuestions.ClientID + "','" + cbxSelectAll.ClientID + "','cbxSelect')");
        }
    }

    protected void Delete_Click(object sender, EventArgs e)
    {
        List<int> questionsToDelete = new List<int>();

        foreach (GridViewRow row in grvPollQuestions.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cbxSelect = (CheckBox)row.FindControl("cbxSelect");
                if (cbxSelect != null && cbxSelect.Checked)
                {
                    Label lblId = (Label)row.FindControl("lblId");
                    questionsToDelete.Add(Convert.ToInt32(lblId.Text));
                }
            }
        }
        PollQuestion.Delete(questionsToDelete);
        BindGrid();
    }

    protected void Activate_Changed(object sender, EventArgs e)
    {
        foreach (GridViewRow oldrow in grvPollQuestions.Rows)
        {
            RadioButton rbtActivate = ((RadioButton)oldrow.FindControl("rbtActivate"));
            if (rbtActivate == (RadioButton)sender)
            {
                rbtActivate.Checked = true;
                int id = Convert.ToInt32(((Label)oldrow.FindControl("lblId")).Text);
                hdfId.Value = id.ToString();
                pnlPollEvent.Visible = true;
            }
            else
                rbtActivate.Checked = false;
        }
    }

    protected void CreateEvent_Click(object sender, EventArgs e)
    {
        if (!PollEvent.IsOverlap(wucStartTime.Date, wucEndTime.Date))
        {
            PollEvent.Create(int.Parse(hdfId.Value), wucStartTime.Date, wucEndTime.Date);
            uccResponse.SetSuccessMessage("Poll event activated");
        }
    }

    protected void ShowAnswerCommand(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "ShowAnswers")
        {
            grvPollAnswer.DataSource = PollAnswer.GetAnswersForQuestion(Convert.ToInt32((e.CommandArgument)));
            grvPollAnswer.DataBind();
        }
    }
}
